package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhentao.mapper.BusinessShopMapper;
import com.zhentao.mapper.BusinessUserMapper;
import com.zhentao.minioService.MinioService;
import com.zhentao.model.common.business.dto.BusinessShopAuditDto;
import com.zhentao.model.common.business.dto.BusinessShopDto;
import com.zhentao.model.common.business.pojo.BusinessShop;
import com.zhentao.model.common.business.pojo.BusinessUser;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.service.BusinessShopService;
import com.zhentao.utils.common.utils.TextModerationPlusDemo;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
* @author liu
* @description 针对表【business_shop】的数据库操作Service实现
* @createDate 2024-09-15 09:31:44
*/
@Service
public class BusinessShopServiceImpl extends ServiceImpl<BusinessShopMapper, BusinessShop>
    implements BusinessShopService {
    @Resource
    private BusinessShopMapper businessShopMapper;
    @Resource
    private BusinessUserMapper businessUserMapper;

    /**
     *  商家列表
     *  根据名称模糊查  分页  根据状态查
     * @param businessShopDto  查询列表--条件
     * @return
     */
    @Override
    public ResponseResult shopList(BusinessShopDto businessShopDto) {
        //进行分页
        PageHelper.startPage(businessShopDto.getPageNum(),businessShopDto.getPageSize());
        //进行条件查
        List<BusinessShop> businessShopList = businessShopMapper.selectList(Wrappers.<BusinessShop>lambdaQuery()
                //根据名称模糊查
                .like(StringUtils.isNotBlank(businessShopDto.getShopName()), BusinessShop::getShopName, businessShopDto.getShopName())
                //根据商家状态查
                .eq(businessShopDto.getShopState() != null , BusinessShop::getShopState, businessShopDto.getShopState())
                //根据商家类型查
                .eq(businessShopDto.getTypeId() != null, BusinessShop::getTypeId, businessShopDto.getTypeId())
                //根据创建时间进行区间查
                .gt(businessShopDto.getStartTime() != null, BusinessShop::getShopCreatetime, businessShopDto.getStartTime())
                .lt(businessShopDto.getEndTime() != null, BusinessShop::getShopCreatetime, businessShopDto.getEndTime())
                //根据店铺审核状态查询
                .eq(businessShopDto.getShopAudit()!=null,BusinessShop::getShopAudit,businessShopDto.getShopAudit())
        );

        for (BusinessShop businessShop : businessShopList) {
            //根据用户id进行查询
            BusinessUser businessUser = businessUserMapper.selectById(businessShop.getUserId());
            businessShop.setBusinessUser(businessUser);
        }


        PageInfo<BusinessShop> businessShopPageInfo = new PageInfo<>(businessShopList);


        return ResponseResult.okResult(businessShopPageInfo);
    }





    /**
     * 根据ID进行对店铺进行信息审核
     *
     * @return
     */
    @Override
    public ResponseResult updShopAudit(BusinessShopAuditDto businessShopAuditDto) {
        //判断有无拒绝原因
        if (businessShopAuditDto.getShopReject()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"请填写拒绝原因");
        }

        //根据店铺id进行查询
        BusinessShop businessShop = businessShopMapper.selectById(businessShopAuditDto.getShopId());
        if (businessShopAuditDto.getButttID()==2){
            //审核拒绝
            businessShop.setShopAudit(2);
            businessShop.setShopReject(businessShopAuditDto.getShopReject());

            businessShopMapper.updateById(businessShop);

            return ResponseResult.okResult("审核拒绝");
        }else{
            //审核通过
            businessShop.setShopAudit(1);

            return ResponseResult.okResult("审核通过");
        }
    }


    /**
     * 店铺详情--根据账号ID进行查看当前的店铺信息
     * @param id
     * @return
     */
    @Override
    public ResponseResult selShopOneById(Integer id) {
        //根据账号ID进行查询店铺信息
        BusinessShop businessShop = businessShopMapper.selectById(id);
        if (businessShop==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }else {
            return ResponseResult.okResult(businessShop);
        }
    }


    @Override
    public ResponseResult addBusiness(BusinessShop businessShop) throws Exception {
        if (businessShop == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        QueryWrapper<BusinessShop> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BusinessShop::getShopName,businessShop.getShopName());
        BusinessShop businessShop1 = this.businessShopMapper.selectOne(queryWrapper);
        if(businessShop1 !=null){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR,"店铺名称已占用");
        }
        String shopName = businessShop.getShopName();
        businessShop.setShopCreatetime(new Date());
        ResponseResult responseResult = TextModerationPlusDemo.AutoText(shopName);
        if (responseResult.getCode() == 600){
            businessShop.setShopAudit(1);
            businessShopMapper.insert(businessShop);
            return ResponseResult.okResult(businessShop);
        }else if (responseResult.getCode() == 601){
            businessShop.setShopAudit(2);
            businessShopMapper.insert(businessShop);
            return ResponseResult.errorResult(601,"审核失败");
        }else {
            businessShop.setShopAudit(3);
            businessShopMapper.insert(businessShop);
            return ResponseResult.errorResult(602,"审核失败");
        }
    }

    @Override
    public ResponseResult findById(Integer userId) {
        QueryWrapper<BusinessShop> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(BusinessShop::getUserId,userId);
        BusinessShop businessShop1 = this.businessShopMapper.selectOne(queryWrapper);
//        BusinessShop businessShop = businessShopMapper.selectById(userId);
        return ResponseResult.okResult(businessShop1);
    }

    @Override
    public ResponseResult update(BusinessShop businessShop) throws Exception {
        if (businessShop == null){
            return ResponseResult.errorResult(400,"参数为空");
        }
        String shopName = businessShop.getShopName();
        ResponseResult responseResult = TextModerationPlusDemo.AutoText(shopName);

        if (responseResult.getCode() == 600){
            businessShop.setShopAudit(1);
            businessShopMapper.updateById(businessShop);
            return ResponseResult.okResult(businessShop);
        }else if (responseResult.getCode() == 601){
            businessShop.setShopAudit(2);
            businessShopMapper.updateById(businessShop);
            return ResponseResult.errorResult(601,"审核失败");
        }else {
            businessShop.setShopAudit(3);
            businessShopMapper.updateById(businessShop);
            return ResponseResult.errorResult(602,"审核失败");
        }
    }

    @Override
    public ResponseResult upstate(Integer shopId) {
        BusinessShop businessShop = businessShopMapper.selectById(shopId);
        if (businessShop.getShopAudit() == 1){
            if (businessShop.getShopState() == 0){
                //开店
                businessShop.setShopState(1);
                businessShopMapper.updateById(businessShop);
            }else if (businessShop.getShopState() == 1){
                businessShop.setShopState(0);
                businessShopMapper.updateById(businessShop);
            }else if (businessShop.getShopState() == 2){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"此商铺已永久关闭");
            }
        }else {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"此商铺未审核通过");
        }
        return ResponseResult.okResult(businessShop);
    }

    @Resource
    private MinioService fileStorageService;
    @SneakyThrows
    @Override
    public ResponseResult upLogo(MultipartFile file) {
        InputStream inputStream=file.getInputStream();
        String s = fileStorageService.uploadHtmlImage(UUID.randomUUID().toString()+".jpg", inputStream);
        System.out.println("上传到minio当中的图片路径："+s);
        return ResponseResult.okResult(s);
    }

}




